<template>
  <div class="my-page">
    <div v-if="!showOrderDetail">
      <header class="header">
        <h1>我的</h1>
      </header>

      <div class="user-info">
        <div class="avatar">
          <img src="https://m11.360buyimg.com/babel/s150x150_jfs/t1/257813/10/10893/73371/6780f9e3Fbafa885c/ce71aab398484726.png.webp" alt="用户头像" />
        </div>
        <div class="info">
          <div class="name">用户昵称</div>
          <div class="level">普通会员</div>
        </div>
        <div class="setting">
          <span class="icon">⚙️</span>
        </div>
      </div>

      <div class="menu-section">
        <div class="menu-header">
          <span>我的订单</span>
          <span class="view-all">查看全部 ></span>
        </div>
        <div class="menu-grid">
          <div class="menu-item">
            <div class="menu-icon">🕒</div>
            <div class="menu-text">待服务</div>
          </div>
          <div class="menu-item">
            <div class="menu-icon">🔄</div>
            <div class="menu-text">进行中</div>
          </div>
          <div class="menu-item">
            <div class="menu-icon">✅</div>
            <div class="menu-text">已完成</div>
          </div>
          <div class="menu-item">
            <div class="menu-icon">💬</div>
            <div class="menu-text">待评价</div>
          </div>
        </div>
      </div>

      <div class="appointment-records">
        <div class="section-header">
          <span>预约记录</span>
        </div>
        
        <div v-if="orders.length === 0" class="empty-records">
          <div class="empty-icon">📅</div>
          <div class="empty-text">暂无预约记录</div>
        </div>
        
        <div v-else class="records-list">
          <div class="record-item" v-for="order in orders" :key="order.id">
            <div class="record-header">
              <div class="service-name">{{ order.serviceName }}</div>
              <div class="service-status" :class="getStatusClass(order.status)">{{ order.status }}</div>
            </div>
            <div class="record-info">
              <div class="info-item">
                <span class="label">预约时间：</span>
                <span>{{ order.serviceDate }} {{ order.serviceTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">服务对象：</span>
                <span>{{ order.patientName }}</span>
              </div>
              <div class="info-item">
                <span class="label">联系电话：</span>
                <span>{{ order.phone }}</span>
              </div>
              <div class="info-item">
                <span class="label">服务地址：</span>
                <span>{{ order.address }}</span>
              </div>
            </div>
            <div class="record-footer">
              <div class="price">¥{{ order.price }}</div>
              <div class="actions">
                <button class="action-btn" @click="cancelOrder(order.id)" v-if="order.status === '待服务'">取消预约</button>
                <button class="action-btn primary" @click="viewOrderDetail(order.id)">查看详情</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <OrderDetail 
      v-else 
      :orderId="selectedOrderId" 
      @go-back="showOrderDetail = false" 
      @order-updated="handleOrderUpdated"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import OrderDetail from './OrderDetail.vue';

const orders = ref([]);
const showOrderDetail = ref(false);
const selectedOrderId = ref('');

onMounted(() => {
  loadOrders();
});

const loadOrders = () => {
  try {
    const savedOrders = JSON.parse(localStorage.getItem('nurseOrders') || '[]');
    orders.value = savedOrders.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
  } catch (error) {
    console.error('加载订单失败:', error);
    orders.value = [];
  }
};

const getStatusClass = (status) => {
  switch (status) {
    case '待服务':
      return 'status-pending';
    case '进行中':
      return 'status-ongoing';
    case '已完成':
      return 'status-completed';
    case '已取消':
      return 'status-cancelled';
    default:
      return '';
  }
};

const cancelOrder = (orderId) => {
  if (confirm('确定要取消此预约吗？')) {
    try {
      let savedOrders = JSON.parse(localStorage.getItem('nurseOrders') || '[]');
      const orderIndex = savedOrders.findIndex(order => order.id === orderId);
      
      if (orderIndex !== -1) {
        savedOrders[orderIndex].status = '已取消';
        localStorage.setItem('nurseOrders', JSON.stringify(savedOrders));
        loadOrders(); // 重新加载订单
      }
    } catch (error) {
      console.error('取消订单失败:', error);
    }
  }
};

const viewOrderDetail = (orderId) => {
  selectedOrderId.value = orderId;
  showOrderDetail.value = true;
};

const handleOrderUpdated = () => {
  loadOrders(); // 订单更新后重新加载订单列表
};
</script>

<style scoped>
.my-page {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  max-width: 750px;
  margin: 0 auto;
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 70px;
}

.header {
  background-color: #00a163;
  color: white;
  padding: 15px 0;
  text-align: center;
}

.header h1 {
  font-size: 18px;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 20px 15px;
  background-color: #00a163;
  color: white;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 15px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.info {
  flex: 1;
}

.name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.level {
  font-size: 14px;
  opacity: 0.8;
}

.setting {
  font-size: 20px;
}

.menu-section {
  background-color: white;
  margin: 10px 0;
  padding: 15px;
}

.menu-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 16px;
}

.view-all {
  color: #999;
  font-size: 14px;
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.menu-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.menu-icon {
  font-size: 24px;
  margin-bottom: 5px;
}

.menu-text {
  font-size: 12px;
  color: #333;
}

.appointment-records {
  background-color: white;
  margin: 10px 0;
  padding: 15px;
}

.section-header {
  margin-bottom: 15px;
  font-size: 16px;
  font-weight: bold;
}

.empty-records {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 0;
  color: #999;
}

.empty-icon {
  font-size: 40px;
  margin-bottom: 10px;
}

.empty-text {
  font-size: 14px;
}

.records-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.record-item {
  border: 1px solid #eee;
  border-radius: 8px;
  overflow: hidden;
}

.record-header {
  display: flex;
  justify-content: space-between;
  padding: 10px 15px;
  background-color: #f9f9f9;
  border-bottom: 1px solid #eee;
}

.service-name {
  font-weight: bold;
}

.service-status {
  font-size: 14px;
}

.status-pending {
  color: #ff9500;
}

.status-ongoing {
  color: #007aff;
}

.status-completed {
  color: #4cd964;
}

.status-cancelled {
  color: #999;
}

.record-info {
  padding: 10px 15px;
}

.info-item {
  display: flex;
  margin-bottom: 5px;
  font-size: 14px;
}

.label {
  color: #666;
  width: 80px;
}

.record-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  border-top: 1px solid #eee;
}

.price {
  font-weight: bold;
  color: #ff4d4f;
}

.actions {
  display: flex;
  gap: 10px;
}

.action-btn {
  padding: 5px 12px;
  border: 1px solid #ddd;
  border-radius: 15px;
  background-color: white;
  font-size: 12px;
  cursor: pointer;
  color: #00a163;
}

.action-btn.primary {
  background-color: #00a163;
  color: white;
  border-color: #00a163;
}

@media screen and (max-width: 375px) {
  .menu-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .info-item {
    flex-direction: column;
  }
  
  .label {
    width: auto;
    margin-bottom: 2px;
  }
}
</style> 